#include<bits/stdc++.h>
using namespace std;

struct node{
    int l,r,real;
};

bool cmp(const node& a,const node& b){
    return a.l<b.l;
}

void solve(){
    int n,cur;
    cin>>n>>cur;
    vector<node> order(n);
    for(int i=0;i<n;i++){
        cin>>order[i].l>>order[i].r>>order[i].real;
    }
    sort(order.begin(),order.end(),cmp);

    int idx = 0,res=cur;
    priority_queue<pair<int, int>> pq;
    
    while (1) {
        while (idx < n && order[idx].l <= cur) {
            node c = order[idx];
            if (c.r >= cur) {
                pq.push({-c.real, c.r});
            }
            idx++;
        }
        
        bool found = false;
        while (!pq.empty()) {
            pair<int,int> temp=pq.top();
            int real = -temp.first;
            res=max(res,real);
            pq.pop();
            
            if (temp.second >= cur) {
                cur = real;
                found = true;
                break;
            }
        }
        
        if (!found) break;
    }
    cout<<res<<'\n';
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
}